/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Fmessage.java
 *
 * Created on 4 mai 2011, 16:14:52
 */

package frame;

import commun.Message;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
<<<<<<< .mine
import java.util.ArrayList;
=======
>>>>>>> .r42
import java.util.Date;
import server._ZoneExchange;
import user.User;

/**
 *
 * @author qiaob
 */
public class Fmessage extends javax.swing.JFrame {

    /** Creates new form Fmessage */
    public Fmessage() {
        initComponents();
    }

<<<<<<< .mine
 
    public Fmessage(_ZoneExchange z,ArrayList messages,String idHost,String idFriend) {
        this.z = z;
        this.idHost=idHost;
        this.initFmessage(z, idFriend);
       // this.lUserInfo.setText("User info : \n"+this.user.getUserName()+"\n " +this.user.getSex());
        initComponents();
        this.setSize(410, 300);
        this.setCenter();
        if(messages!=null)
        {
          this.displayMessages(messages);
        }
=======
    public Fmessage(_ZoneExchange z,String idHost,String idFriend)
    {
        this.z = z;
        this.idHost = idHost;
        this.initFmessage(z, idFriend);
//        this.lUserInfo.setText("User info");
       initComponents();

    }
    public Fmessage(_ZoneExchange z,Message message) {
        this.z = z;
        this.idHost = message.getToId();
        this.initFmessage(z, message.getFromId());
        this.lUserInfo.setText("User info : \n"+this.user.getUserName()+"\n " +this.user.getSex());
        initComponents();
>>>>>>> .r42
    }

<<<<<<< .mine

private void displayMessages(ArrayList msg)
 {
       for(int i=0;i<msg.size();i++)
       {
         this.displayMsg((Message) msg.get(i));

       }
}

private void setCenter() {
		Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension thisSize = getSize();
		setLocation((scrSize.width - thisSize.width) / 2,
				(scrSize.height - thisSize.height) / 2);
	}
 public void initFmessage(_ZoneExchange z, String idFriend)
 {
          try{
          this.user=z.getUserById(idFriend);
          }
          catch(Exception e)
          {
           e.printStackTrace();
          }

 }

 public void displayMsg(Message msg){

        String tmp= msg.getDate() + this.user.getUserName()+"say : \n"+msg.getMessage()+"\n";
        this.textAreaGetter.setText(this.textAreaGetter.getText()+tmp);
    }
 public boolean sendMsg(Message msg)
    {
       
       try
         {
         this.z.sendMessage(msg);
        }
         catch(Exception e)
         {
          e.printStackTrace();
         }
         return true;
 }
=======

 public void initFmessage(_ZoneExchange z, String idFriend)
 {
          try{
          this.user=z.getUserById(idFriend);
          }
          catch(Exception e)
          {
           e.printStackTrace();
          }

 }

 public void displayMsg(Message msg){

        String tmp= msg.getDate() + this.user.getUserName()+"say : \n"+msg.getMessage()+"\n";
        this.textAreaGetter.setText(this.textAreaGetter.getText()+tmp);
    }
 public boolean sendMsg(Message msg)
    {
       
       try
         {
         this.z.sendMessage(msg);
        }
         catch(Exception e)
         {
          e.printStackTrace();
         }
         return true;
 }
>>>>>>> .r42
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        textAreaGetter = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        textAreaSenter = new javax.swing.JTextField();
        bSend = new javax.swing.JButton();
        lUserInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        textAreaGetter.setColumns(20);
        textAreaGetter.setEditable(false);
        textAreaGetter.setRows(5);
        jScrollPane1.setViewportView(textAreaGetter);

        bSend.setText("Envoyer");
        bSend.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                bSendMousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(lUserInfo)
                .addGap(85, 85, 85))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textAreaSenter, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addComponent(bSend)
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(lUserInfo)))
                .addGap(3, 3, 3)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textAreaSenter, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bSend))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bSendMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bSendMousePressed
        // TODO add your handling code here:
        String tmp=this.textAreaSenter.getText();
        if(!tmp.equals("") && tmp!=null)
        {
            Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yy hh':'mm");
       String laDate = dateFormat.format(date).toString();
       Message msg=new Message(this.idHost,this.user.getUserId(),tmp,laDate);
            if(this.sendMsg(msg))
            {
                   this.displayMsg(msg);
                   this.textAreaSenter.setText("");
            }
        }

    }//GEN-LAST:event_bSendMousePressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Fmessage().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bSend;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lUserInfo;
    private javax.swing.JTextArea textAreaGetter;
    private javax.swing.JTextField textAreaSenter;
    // End of variables declaration//GEN-END:variables

    private User user;
    private _ZoneExchange z;
    private String idHost;

}
